﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using Model;
namespace DAL
{
    public class MemberDAL
    {

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public List<Member> GetList()
        {
            return DBUtility.MySqlHelper.ExecuteDataReader<Member>("select * from member where mem_status=0");
        }

        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <param name="loginname"></param>
        /// <returns></returns>
        public Member GetModel(string loginname)
        {
            return DBUtility.MySqlHelper.ExecuteDataReaderModel<Member>("select * from member where mem_status=0 and mem_login=@loginname", new MySqlParameter("@loginname", loginname));
        }

        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <param name="memid"></param>
        /// <returns></returns>
        public Member GetModel(int memid)
        {
            return DBUtility.MySqlHelper.ExecuteDataReaderModel<Member>("select * from member where mem_status=0 and mem_id=@memid", new MySqlParameter("@memid", memid));
        }

        /// <summary>
        /// 获取自定义数据
        /// </summary>
        /// <param name="columns">列名</param>
        /// <param name="where">条件 </param>
        /// <returns></returns>
        public List<dynamic> GetList(string column, string where)
        {
            string sql = string.Format("select {0} from member where 1=1 {1}", column, where);
            return DBUtility.MySqlHelper.ExecuteDynamic(sql);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Member model)
        {
            MySqlParameter[] para = {
                new MySqlParameter("@mem_login",model.mem_login),
                new MySqlParameter("@mem_pwd",model.mem_pwd),
                new MySqlParameter("@mem_level",model.mem_level),
                new MySqlParameter("@mem_name",model.mem_name),
                new MySqlParameter("@mem_count",model.mem_count),
                new MySqlParameter("@mem_status",model.mem_status)
            };
            return DBUtility.MySqlHelper.ExecuteNonQuery(" insert into member (mem_login,mem_pwd,mem_level,mem_name,mem_count,mem_status) Values(@mem_login,@mem_pwd,@mem_level,@mem_name,@mem_count,@mem_status)", para);

        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(Member model)
        {
            MySqlParameter[] para = {
                new MySqlParameter("@mem_login",model.mem_login),
                new MySqlParameter("@mem_pwd",model.mem_pwd),
                new MySqlParameter("@mem_level",model.mem_level),
                new MySqlParameter("@mem_name",model.mem_name),
                new MySqlParameter("@mem_id",model.mem_id)
            };
            return DBUtility.MySqlHelper.ExecuteNonQuery("update member set mem_login=@mem_login,mem_pwd=@mem_pwd,mem_level=@mem_level,mem_name=@mem_name where mem_id=@mem_id", para);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update2(Member model)
        {
            MySqlParameter[] para = {
                new MySqlParameter("@mem_login",model.mem_login),
                new MySqlParameter("@mem_level",model.mem_level),
                new MySqlParameter("@mem_name",model.mem_name),
                new MySqlParameter("@mem_id",model.mem_id)
            };
            return DBUtility.MySqlHelper.ExecuteNonQuery("update member set mem_login=@mem_login,mem_level=@mem_level,mem_name=@mem_name where mem_id=@mem_id",para);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int delete(int id)
        {
            return DBUtility.MySqlHelper.ExecuteNonQuery("update member set mem_status=1 where mem_id=@id", new MySqlParameter("@id", id));
        }

    }
}
